{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据分析",
   "id": "d9538a8b309140aa"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数据收集",
   "id": "7da5e08cc78f27c3"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#数据的导入\n",
    "import pandas as pd\n",
    "#csv\n",
    "df = pd.read_csv('data/sample_data.csv')\n",
    "#json\n",
    "df1 = pd.read_json('data/sample.json')\n",
    "import json\n",
    "with open('data/sample.json') as f:\n",
    "    data = json.load(f)\n",
    "df1 = pd.DataFrame(data['users'])\n",
    "df1\n",
    "#导出\n",
    "#df.to_csv('填写新建文件的地址:data/---.文件后缀')\n",
    "#df.to_json(...)\n"
   ],
   "id": "4a6aaed20027aca0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数据清洗",
   "id": "657137d23c5f6c5"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#缺失值处理\n",
    "##查看是否是缺失值\n",
    "print(df.isna())\n",
    "print(df.isnull())\n",
    "##查看缺失值的个数\n",
    "print(df.isnull().sum())\n",
    "##去掉缺失值\n",
    "print(df.dropna())#剔除一整条的记录\n",
    "print(df.dropna(thresh = 2))#每行如果至少有n个值不是缺失值,就保留\n",
    "print(df.dropna(axis = 1))#剔除一整列的记录\n",
    "print(df.dropna(how='all'))#如果所有的值都是缺失值,删除这一行\n",
    "print(df.dropna(subset = ['user_id']))#如果某列有缺失值,则删除这一行\n",
    "##填充缺失值\n",
    "print(df1.fillna({'signup_date':'2023-6-15'}))#用字典填充\n",
    "print(df1.fillna({'is_active':df1['is_active'].mode()[0]}))#用统计值填充\n",
    "print(df1.ffill())#用前面的相邻值填充\n",
    "print(df1.bfill())#用后面的相邻值填充"
   ],
   "id": "ff2a577438c7f865",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "print(df1.columns)",
   "id": "b55ce33f5959f898",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#检测重复内容\n",
    "df.duplicated()\n",
    "#去重\n",
    "df1.drop_duplicates(subset = ['name'],keep = 'last')#保留最后一次出现的行"
   ],
   "id": "10949c392da69fa9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#数据类型的转换\n",
    "df1['user_id'] = df['user_id'].astype('int16')"
   ],
   "id": "333074a9422d65cc",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#数据变形\n",
    "#宽表转换成长表\n",
    "df2 = pd.melt(df1, id_vars = ['user_id','name'], var_name = '其他',value_name = '详细信息')\n",
    "df2"
   ],
   "id": "6b890d69353a190a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-31T01:42:25.190227Z",
     "start_time": "2025-07-31T01:42:25.174182Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "df1 = pd.read_json('data/sample.json')\n",
    "import json\n",
    "with open('data/sample.json') as f:\n",
    "    data = json.load(f)\n",
    "df1 = pd.DataFrame(data['users'])\n",
    "#分列\n",
    "df1[['first','last']]=df1['name'].str.split(\" \",expand = True)"
   ],
   "id": "aa0e9c6360264fa3",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数据分析",
   "id": "7a0a69124d52e913"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数据可视化",
   "id": "f48f3b3207fd11d2"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
